<?php

namespace App\Http\Controllers\Wap;

use App\Models\AdminAppraisal;
use App\Models\AdminParam;
use App\Models\Car;
use App\Models\Order;
use App\Models\User;
use Illuminate\Http\Request;

class OrdersController extends WapController
{

    // 下订单
    public function store(Request $request, Order $order, Car $carModel)
    {
        $uid = $request->get('user');
        $user = User::find($uid->id);
        $request_data = $request->all();
        $type = $request_data['type'];
        $car_id = $request_data['car_id'];
        $car = $carModel::where('id', $car_id)->where('status', 1)->first();
        if (!$car) {
            return $this->message('车辆已下架');
        }
        // 判断订单是否已经存在
        $is_order = $order->where('car_id', $car_id)->where('type', $type)->where('user_id', $user->id)->first();
        if ($is_order) {
            $order_info = $is_order->with('items')->find($is_order->id)->makeHidden('agreement');
            return $this->success($order_info);
        }

        $order = \DB::transaction(function () use ($user, $type, $car, $request) {
            // 查询运营参数
            $params = AdminParam::find(1);
            if ($type == 1) {
                // 诊断订单
                $orderInfo = [
                    'type'         => $type,
                    'car_id'       => $car->id,
                    'total_amount' => $params->diagnosis_price,     // 总价
                    'pay_amount'   => $params->diagnosis_price
                ];
            } elseif ($type == 2) {
                // 如果是鉴定的订单，接受支付协议图
                $appraisal = AdminAppraisal::where('status', 1)->where('low_price', '<=', $car->new_price)->where('high_price', '>', $car->new_price)->first();
                // 检测用户是否有优惠券   coupon_use = 0 表示未使用
                if ($user->coupon != 0 && $user->coupon_use == 0 && ($appraisal->price >= $user->coupon)) {
                    $coupon = $user->coupon;
                } else {
                    $coupon = 0;
                }
                $orderInfo = [
                    'type'          => $type,
                    'car_id'        => $car->id,
                    'total_amount'  => $appraisal->price,           // 总价
                    'coupon_amount' => $coupon,                     // 优惠券
                    'pay_amount'    => ($appraisal->price) - $coupon,    // 支付价格
                    'agreement'     => $request->post('agreement'),        // 接收的协议， text文本
                    'bespeak'       => isset($request->bespeak) ? 1 : 0,      // 是否预约
                ];
            } else {
                return $this->failed('订单类型缺失');
            }
            $order = new Order($orderInfo);
            // 订单关联到当前用户
            $order->user()->associate($user);
            // 写入数据库
            $order->save();

            // 创建订单快照
            $order_items = $order->items()->make([
                'brand'            => $car->brand,
                'series'           => $car->series,
                'frame_no'         => $car->frame_no,
                'new_price'        => $car->new_price,
                'mileage'          => $car->mileage,
                'first_at'         => $car->first_at,
                'filed_at'         => $car->filed_at,
                'diagnosis_at'     => $car->diagnosis_at,
                'engineer'         => $car->engineer,
                'engineer_comment' => $car->engineer_comment,
                'diagnosis_report' => $car->diagnosis_report,
                'appraisal_report' => $car->appraisal_report,
            ]);
            $order_items->save();

            $order->items = $order_items;
            return $this->success($order->makeHidden('agreement'));
        });

        return $order;
    }
}
